import random


class Level(object):
    """
    等级类 1-10级
    """

    def __init__(self):
        self.__level = '1'
        self.__experience_dic = {
            '1': 10000,
            '2': 30000,
            '3': 60000,
            '4': 100000,
            '5': 150000,
            '6': 250000,
            '7': 400000,
            '8': 600000,
            '9': 850000,
            '10': 1000000,
        }

    @property
    def level(self):
        return self.__level

    @level.setter
    def level(self, experience):
        if experience > self.__experience_dic['10']:
            return

        if self.__experience_dic[self.__level] <= experience:
            # print(experience, '>=', self.__experience_dic[self.__level])
            self.__level = str(int(self.__level) + 1)
        else:
            pass


# class Track(object):
#     """
#     曲目总分分数
#     """
#


class User(object):
    """
    用户基类 拥有星币 积分 勋章属性
            用有练习 演奏方法
    """
    def __init__(self, name):
        self.name = name
        # 私有属性 星币 经验 勋章
        self.__level = Level()
        self.__currency = None
        self.__experience = 0
        self.__medal = None
        self.__history = list()

    @property
    def currency(self):
        return self.__currency

    @property
    def experience(self):
        return self.__experience

    @property
    def medal(self):
        return self.medal

    @property
    def level(self):
        result = '**LEVEL**\n\tuser: %s\n\tlevel: %s\n\texperience: %s\n' % \
                 (self.name, self.__level.level, self.__experience)
        return result

    @property
    def history(self):
        avg_rate = round(sum([item[1] for item in self.__history]) / len(self.__history))
        avg_experience = round(sum([item[2] for item in self.__history]) / len(self.__history))

        result = '**HISTORY**\n\tuser: %s\n\tpractise num: %s\n\tavg rate: %s\n\tavg exp: %s\n' % \
                 (self.name, len(self.__history), str(avg_rate)+'%', avg_experience)

        return result

    def __add_experience(self, experience):
        if 0 > experience or experience > 1150000:
            print('experience is error data %s' % experience)
            return
        self.__experience += experience
        self.__level.level = self.__experience

    def practise(self, tracks):
        track_num = 0
        while track_num < tracks:
            track_level = random.randint(1, 6)
            # track_level = 1
            correct_rate = random.randrange(2, 10) / 10
            # correct_rate = 0.9
            experience = round(track_level/10 * 10000 * correct_rate / int(self.__level.level))
            # experience = round(track_level/10 * 10000 * correct_rate / 1)
            self.__history.append((track_level, correct_rate * 100, experience))
            self.__add_experience(experience)
            # print(self.name, 'practise, track level: %s correct rate: %s prac experience: %s user level %s exp: %s'
            #       % (track_level, correct_rate, experience, self.__level.level, self.__experience))
            track_num += 1

    def play(self):
        pass


if __name__ == '__main__':
    user1 = User('user1')
    user2 = User('user2')
    print(user1.level)
    print(user2.level)
    user1.practise(300)
    user2.practise(3000)
    print(user1.level)
    print(user2.level)
    print(user1.history)
    print(user2.history)




